﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Documents;
using Kokomo.Controls;

namespace Kokomo
{
	public static class WatermarkService
	{
		public static readonly object WatermarkTemplateKey = new ComponentResourceKey(typeof(WatermarkService), "WatermarkTemplateKey");

		public static string GetWatermarkContent(DependencyObject obj)
		{
			return (string)obj.GetValue(WatermarkContentProperty);
		}

		public static void SetWatermarkContent(DependencyObject obj, string value)
		{
			obj.SetValue(WatermarkContentProperty, value);
		}

		// Using a DependencyProperty as the backing store for WatermarkContent.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty WatermarkContentProperty =
			DependencyProperty.RegisterAttached("WatermarkContent", typeof(string), typeof(WatermarkService), new UIPropertyMetadata(null, OnWatermarkContentChanged));



		private static void OnWatermarkContentChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
		{
			UIElement element = target as UIElement;
			if (element != null)
			{
				EnsureAdorner(element);
			}
		}

		private static void EnsureAdorner(UIElement element)
		{
			WatermarkAdorner adorner = GetAdorner(element);

			if (adorner == null)
			{
				AdornerLayer layer = AdornerLayer.GetAdornerLayer(element);
				if (layer != null)
				{
					Adorner watermark = new WatermarkAdorner(element);
					layer.Add(watermark);
				}
				SetAdorner(element, adorner);
			}
		}

		private static WatermarkAdorner GetAdorner(DependencyObject obj)
		{
			return (WatermarkAdorner)obj.GetValue(AdornerProperty);
		}

		private static void SetAdorner(DependencyObject obj, WatermarkAdorner value)
		{
			obj.SetValue(AdornerProperty, value);
		}

		// Using a DependencyProperty as the backing store for Adorner.  This enables animation, styling, binding, etc...
		private static readonly DependencyProperty AdornerProperty =
			DependencyProperty.RegisterAttached("Adorner", typeof(WatermarkAdorner), typeof(WatermarkService), new UIPropertyMetadata(null));



		public static DataTemplate GetWatermarkTemplate(DependencyObject obj)
		{
			return (DataTemplate)obj.GetValue(WatermarkTemplateProperty);
		}

		public static void SetWatermarkTemplate(DependencyObject obj, DataTemplate value)
		{
			obj.SetValue(WatermarkTemplateProperty, value);
		}

		// Using a DependencyProperty as the backing store for WatermarkTemplate.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty WatermarkTemplateProperty =
			DependencyProperty.RegisterAttached("WatermarkTemplate", typeof(DataTemplate), typeof(WatermarkService), new UIPropertyMetadata(null));


	}
}
